using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class CTMuaHangDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SaveCTMuaHang(CTMuaHang ctmuahang)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddCTMuaHang", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddCTMuaHang";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, ctmuahang.Id);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdMuaHang", DbType.Int32, ParameterDirection.Input, ctmuahang.IdMuaHang);
			cmd.Parameters.Add(param);
			param = CreateParameter("@TenHang", DbType.String, ParameterDirection.Input, ctmuahang.TenHang);
			cmd.Parameters.Add(param);
            param = CreateParameter("@MaHang", DbType.String, ParameterDirection.Input, ctmuahang.MaHang);
            cmd.Parameters.Add(param);
			param = CreateParameter("@DonViTinh", DbType.String, ParameterDirection.Input, ctmuahang.DonViTinh);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoLuong", DbType.Int64, ParameterDirection.Input, ctmuahang.SoLuong);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DonGia", DbType.Int64, ParameterDirection.Input, ctmuahang.DonGia);
			cmd.Parameters.Add(param);
            param = CreateParameter("@IsDelete", DbType.Int32, ParameterDirection.Input, ctmuahang.IsDelete);
            cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}
		
		// Get all CTMuaHang in DB
		public ResultObj GetAllCTMuaHang( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<CTMuaHang> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllCTMuaHang", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<CTMuaHang>();
			CTMuaHang item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new CTMuaHang();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdMuaHang = Convert.ToInt32(reader["IdMuaHang"]);
			item.TenHang = Convert.ToString(reader["TenHang"]);
            item.MaHang = Convert.ToString(reader["MaHang"]);
			item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
			item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all CTMuaHang in DB with condition
		public ResultObj GetAllCTMuaHangByID(CTMuaHang ctmuahang)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<CTMuaHang> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllCTMuaHangByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, ctmuahang.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<CTMuaHang>();
			CTMuaHang item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new CTMuaHang();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdMuaHang = Convert.ToInt32(reader["IdMuaHang"]);
			item.TenHang = Convert.ToString(reader["TenHang"]);
            item.MaHang = Convert.ToString(reader["MaHang"]);
			item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
			item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a CTMuaHang out of DB with condition
		public ResultObj DeleteCTMuaHang(CTMuaHang ctmuahang)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeleteCTMuaHang", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeleteCTMuaHang";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, ctmuahang.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
		
		// Get info of MuaHang from DB with condition
		public ResultObj GetMuaHang(int IdMuaHang)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<MuaHang> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetMuaHang", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdMuaHang);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<MuaHang>();
			MuaHang item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new MuaHang();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.MaSo = Convert.ToString(reader["MaSo"]);
			item.NguoiLap = Convert.ToString(reader["NguoiLap"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.NhaCungCap = Convert.ToString(reader["NhaCungCap"]);
			item.DiaChi = Convert.ToString(reader["DiaChi"]);
			item.NguoiGiaoHang = Convert.ToString(reader["NguoiGiaoHang"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			item.DienGiai = Convert.ToString(reader["DienGiai"]);
			item.ChungTuKemTheo = Convert.ToString(reader["ChungTuKemTheo"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
					
	}
}
